﻿/*----------------------------------------------------------------
//  <copyright file="HttpContextServiceScopeFactory.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2022-11-15 11:23</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.AspNetCore
{
    #region "基于当前HttpContext的<see cref="IServiceScope"/>工厂"
    /// <summary>
    /// 基于当前HttpContext的<see cref="IServiceScope"/>工厂，
    /// 如果当前操作处于HttpRequest作用域中，直接使用HttpRequest的作用域，否则创建新的作用域
    /// </summary>
    public class HttpContextServiceScopeFactory : IHybridServiceScopeFactory
    {
        #region "属性"
        #region "获取 包装的<see cref="IServiceScopeFactory"/>"
        /// <summary>
        /// 获取 包装的<see cref="IServiceScopeFactory"/>
        /// </summary>
        public IServiceScopeFactory ServiceScopeFactory { get; }
        #endregion
        #region "获取 当前请求的<see cref="IHttpContextAccessor"/>对象"
        /// <summary>
        /// 获取 当前请求的<see cref="IHttpContextAccessor"/>对象
        /// </summary>
        protected IHttpContextAccessor HttpContextAccessor { get; }
        #endregion

        #endregion

        #region "构造函数"
        #region "初始化一个<see cref="HttpContextServiceScopeFactory"/>类型的新实例"
        /// <summary>
        /// 初始化一个<see cref="HttpContextServiceScopeFactory"/>类型的新实例
        /// </summary>
        /// <param name="serviceScopeFactory"></param>
        /// <param name="httpContextAccessor"></param>
        public HttpContextServiceScopeFactory(IServiceScopeFactory serviceScopeFactory, IHttpContextAccessor httpContextAccessor)
        {
            ServiceScopeFactory = serviceScopeFactory;
            HttpContextAccessor = httpContextAccessor;
        }
        #endregion

        #endregion

        #region "方法 - Implementation of IServiceScopeFactory"
        #region "创建依赖注入服务的作用域"
        /// <summary>
        /// 创建依赖注入服务的作用域，如
        /// 果当前操作处于HttpRequest作用域中，直接使用HttpRequest的作用域，否则创建新的作用域
        /// </summary>
        /// <returns></returns>
        public virtual IServiceScope CreateScope()
        {
            HttpContext httpContext = HttpContextAccessor?.HttpContext;
            //不在HttpRequest作用域中
            if (httpContext == null)
            {
                return ServiceScopeFactory.CreateScope();
            }

            return new NonDisposedHttpContextServiceScope(httpContext.RequestServices);
        }
        #endregion

        #endregion

        #region "当前HttpRequest的<see cref="IServiceScope"/>的包装"
        /// <summary>
        /// 当前HttpRequest的<see cref="IServiceScope"/>的包装，
        /// 保持HttpContext.RequestServices的可传递性，并且不释放
        /// </summary>
        protected class NonDisposedHttpContextServiceScope : IServiceScope
        {
            #region "属性"
            #region "获取 当前HttpRequest的<see cref="IServiceProvider"/>"
            /// <summary>
            /// 获取 当前HttpRequest的<see cref="IServiceProvider"/>
            /// </summary>
            public IServiceProvider ServiceProvider { get; }
            #endregion

            #endregion

            #region "构造函数"
            #region "初始化一个<see cref="NonDisposedHttpContextServiceScope"/>类型的新实例"
            /// <summary>
            /// 初始化一个<see cref="NonDisposedHttpContextServiceScope"/>类型的新实例
            /// </summary>
            /// <param name="serviceProvider"></param>
            public NonDisposedHttpContextServiceScope(IServiceProvider serviceProvider)
            {
                ServiceProvider = serviceProvider;
            }
            #endregion

            #endregion

            #region "方法"
            #region "释放资源 - 无执行空方法"
            /// <summary>
            /// 释放资源
            /// 因为是HttpContext的，啥也不做，避免在using使用时被释放
            /// </summary>
            public void Dispose()
            { }
            #endregion

            #endregion

        }
        #endregion

    }
    #endregion

}
